//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.service.impl;

import ciicos.base.UserInfo;
import ciicos.base.entity.SuperBaseEntity;
import ciicos.model.AppPositionVO;
import ciicos.model.AppUserInfoVO;
import ciicos.model.AppUsersVO;
import ciicos.permission.entity.OrganizeEntity;
import ciicos.permission.entity.PermissionEntityBase;
import ciicos.permission.entity.PositionEntity;
import ciicos.permission.entity.RoleEntity;
import ciicos.permission.entity.UserEntity;
import ciicos.permission.entity.UserRelationEntity;
import ciicos.permission.service.OrganizeService;
import ciicos.permission.service.PositionService;
import ciicos.permission.service.RoleService;
import ciicos.permission.service.UserRelationService;
import ciicos.permission.service.UserService;
import ciicos.service.AppService;
import ciicos.util.JsonUtil;
import ciicos.util.RedisUtil;
import ciicos.util.StringUtil;
import ciicos.util.UploaderUtil;
import ciicos.util.UserProvider;
import cn.hutool.core.collection.ListUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppServiceImpl implements AppService {
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private UserService userService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private OrganizeService organizeService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserRelationService userRelationService;

    public AppUsersVO userInfo() {
        UserInfo userInfo = this.userProvider.get();
        UserEntity userEntity = this.userService.getInfo(userInfo.getUserId());
        AppUsersVO usersVO = new AppUsersVO();
        usersVO.setBirthday(userEntity.getBirthday() != null ? userEntity.getBirthday().getTime() : null);
        usersVO.setEmail(userEntity.getEmail());
        usersVO.setGender(userEntity.getGender());
        usersVO.setMobilePhone(userEntity.getMobilePhone());
        this.data(usersVO, userEntity, userInfo);
        this.userInfo(usersVO, userInfo);
        PositionEntity position = this.positionService.getInfo(userEntity.getPositionId());
        AppPositionVO positionVO = new AppPositionVO();
        if (position != null) {
            positionVO.setId((String)position.getId());
            positionVO.setName(position.getFullName());
            usersVO.setPositionIds(ListUtil.toList(new AppPositionVO[]{positionVO}));
        }

        if (StringUtil.isNotEmpty(userEntity.getManagerId())) {
            UserEntity menager = this.userService.getInfo(userEntity.getManagerId());
            usersVO.setManager(menager != null ? menager.getRealName() + "/" + menager.getAccount() : "");
        }

        List<String> roles = this.roleService.getAllRoleIdsByUserIdAndOrgId(userInfo.getUserId(), usersVO.getOrganizeId());
        List<RoleEntity> roleList = this.roleService.getListByIds(roles);
        usersVO.setRoleName(String.join("，", (Iterable)roleList.stream().map(PermissionEntityBase::getFullName).collect(Collectors.toList())));
        usersVO.setRoleId(String.join(".", (Iterable)roleList.stream().map(SuperBaseEntity.SuperIBaseEntity::getId).collect(Collectors.toList())));
        return usersVO;
    }

    public AppUserInfoVO getInfo(String id) {
        AppUserInfoVO userInfoVO = new AppUserInfoVO();
        UserEntity entity = this.userService.getInfo(id);
        if (entity != null) {
            userInfoVO = (AppUserInfoVO)JsonUtil.getJsonToBean(entity, AppUserInfoVO.class);
            List<String> positionIds = (List<String>)(StringUtil.isNotEmpty(entity.getPositionId()) ? Arrays.asList(entity.getPositionId().split(",")) : new ArrayList());
            List<String> positionName = (List)this.positionService.getPositionName(positionIds).stream().map((t) -> t.getFullName()).collect(Collectors.toList());
            userInfoVO.setPositionName(String.join(",", positionName));
            OrganizeEntity info = this.organizeService.getInfo(entity.getOrganizeId());
            userInfoVO.setOrganizeName(info != null ? info.getFullName() : "");
            userInfoVO.setHeadIcon(UploaderUtil.uploaderImg(userInfoVO.getHeadIcon()));
        }

        return userInfoVO;
    }

    private void userInfo(UserInfo userInfo, final String userId, boolean isAdmin, UserEntity userEntity) {
        List<String> userIdList = new ArrayList() {{add(userId);}};
        List<UserRelationEntity> data = this.userRelationService.getListByUserIdAll(userIdList);
        List<String> positionList = (List)data.stream().filter((m) -> "Position".equals(m.getObjectType())).map((t) -> t.getObjectId()).collect(Collectors.toList());
        Set<String> id = new LinkedHashSet();
        String[] position = StringUtil.isNotEmpty(userEntity.getPositionId()) ? userEntity.getPositionId().split(",") : new String[0];
        List<String> positions = (List)positionList.stream().filter((t) -> Arrays.asList(position).contains(t)).collect(Collectors.toList());
        id.addAll(positions);
        id.addAll(positionList);
        userInfo.setPositionIds((String[])id.toArray(new String[id.size()]));
        if (!isAdmin) {
            data = (List)data.stream().filter((m) -> "Role".equals(m.getObjectType())).collect(Collectors.toList());
        }

        List<String> roleList = (List)data.stream().map((t) -> t.getObjectId()).collect(Collectors.toList());
        userInfo.setRoleIds(roleList);
    }

    private void data(AppUsersVO usersVO, UserEntity userEntity, UserInfo userInfo) {
        usersVO.setOrganizeId(userEntity.getOrganizeId());
        List<OrganizeEntity> organizeIdList = new ArrayList();
        this.organizeService.getOrganizeId(userEntity.getOrganizeId(), organizeIdList);
        Collections.reverse(organizeIdList);
        usersVO.setOrganizeName((String)organizeIdList.stream().map(PermissionEntityBase::getFullName).collect(Collectors.joining("/")));
        OrganizeEntity organizeEntity = (OrganizeEntity)
                organizeIdList.stream().filter((t) -> (
                                (String)t.getId()).equals(userEntity.getOrganizeId())
                ).findFirst().get();
        if (organizeEntity != null) {
            String[] organizeId = StringUtil.isNotEmpty(organizeEntity.getOrganizeIdTree()) ? organizeEntity.getOrganizeIdTree().split(",") : new String[0];
            if (organizeId.length > 0) {
                userInfo.setOrganizeId(organizeId[0]);
                userInfo.setDepartmentId(organizeId[organizeId.length - 1]);
            }
        }

        userInfo.setManagerId(userInfo.getManagerId());
        boolean b = userInfo.getIsAdministrator();
        List<String> subordinateIdsList = (List)this.userService.getListByManagerId(userInfo.getUserId(), (String)null).stream().map(SuperBaseEntity.SuperIBaseEntity::getId).collect(Collectors.toList());
        userInfo.setSubordinateIds(subordinateIdsList);
        this.userInfo(userInfo, userInfo.getUserId(), b, userEntity);
        userInfo.setSubOrganizeIds(new String[0]);
        UserProvider.setLoginUser(userInfo);
        UserProvider.setLocalLoginUser(userInfo);
    }

    private void userInfo(AppUsersVO appUsersVO, UserInfo userInfo) {
        appUsersVO.setUserId(userInfo.getUserId());
        appUsersVO.setHeadIcon(UploaderUtil.uploaderImg(userInfo.getUserIcon()));
        appUsersVO.setUserName(userInfo.getUserName());
        appUsersVO.setUserAccount(userInfo.getUserAccount());
    }
}
